<script type="text/javascript">
$(document).ready(function(){
  if($("#LinkCategoryId").val()>0){
    $('#cat-selection div ul li #cat_id-'+$("#LinkCategoryId").val()).addClass('selected');
  }
  
  $("#cat-selection div ul li a").click(function(){ 
    $("#cat-selection div ul li a").removeClass('selected');
    var temp = new Array();
    temp = $(this).addClass('selected').attr('id').split('-');
    $("#LinkCategoryId").val(temp[1]);
    return false;
  })
    
  var options = {
	'maxCharacterSize': 500,
	'textFontSize': '13px',
	'textColor': '#5C2306',
	'textFamily': 'Tahoma,sans-serif',
	'textAlign': 'right',
	'warningColor': '#CC3300',  
	'warningNumber': 40,
	'isCharacterCount': true,
	'isWordCount': false,
	'charSpan': ''
  };
  $('#LinkContent').textareaCount(options);
})
</script>
<?php echo $this->Html->script('jquery.textareaCounter.plugin',false); ?>

<div style="padding:0 80px">
<?php 

if($paso==2){
	echo $this->Form->create('Link');
?><fieldset>
	<legend><?php __('Link information'); ?></legend>
   
        	<p class="genericformtxt">
          	  <?=$this->Html->tag('b',$pageObj->title) ?><br />
          	  <?=$this->Html->tag('span',$pageObj->description,array('escape'=>true)) ?><br />
          	  <?=$this->Html->tag('small',$this->data['Link']['url'],array('escape'=>true,'style'=>'color:red')) ?>
          	  
        	</p>
   <?php      
        if($warning) echo '<p class="error">'.$warning.'</p>';
        ?> 
    </fieldset>
    <fieldset>
	<legend><?php __('Links details'); ?></legend>

	<?php 
		
		echo $this->Form->input('title');
		echo $this->Form->input('tags');
		echo $this->Form->hidden('type');
		echo $this->Form->input('content');
		echo $this->Form->input('paso',array('type'=>'hidden','value'=>2));
				
	?>
    </fieldset>
        
    <? if(count(array_merge($fetchImg,$toFetchImg))>0){     
	    	    	
		$options=array(''=>$html->image('no-photo.jpg').'<br />Sin imagen');
		
		$attributes=array('escape'=>false,'default'=>'text','legend'=>__('Select a thumbnail',true));
		echo '<div id="imageRadio">'.$this->Form->radio('thumb',$options,$attributes).'</div>';


		$this->Js->domReady('
		function addImageRatio(key){
			$("#imageRadio fieldset").append(\'<div class="img-div"><input type="radio" value="\'+key+\'" id="LinkThumb\'+key+\'" name="data[Link][thumb]"><label for="LinkThumb\'+key+\'"><img src="'.Router::url('/img/link/tmp/').'\'+key+\'.jpg"></label></div>\');
        }
		$.each('.$this->Js->value($fetchImg).',function(e,key){ addImageRatio(key); } );
		$.each('.$this->Js->value($toFetchImg).',function(e,key){ 
		  $.ajax({
  			url: "'.Router::url(array('controller'=>'links','action'=>'fetchImage')).'/",
  			data: { url: "'.$this->data['Link']['url'].'", key : key },
  			type: "post",
  			dataType: "json",
  			success: function(data){ 
				if(data) addImageRatio(key) 
			}		
		  }) });',true);
	//@todo que vaya consiguiendo de a una imagen no todas a la vez
	} ?>
	
	<fieldset>
	<legend><?php __('Select a category'); ?></legend>
    	
	<?php 
	if(isset($this->validationErrors['Link']['category_id'])){
		echo '<div class="error-message">'.$this->validationErrors['Link']['category_id'].'</div>';
	}	
	?>
	<div id="cat-selection">
	<?php 
	$CATS=ClassRegistry::init('Category')->getCats();
	foreach ($CATS as $cat){
	?>
	  <div>
	    <h4><?=$cat['Category']['name'] ?></h4>
	    <ul>
	    <?php foreach ($cat['children'] as $subcat){
	    	$subcat=$subcat['Category']; 
	    ?>
	      <li><a id="cat_id-<?=$subcat['id'] ?>"><?=$subcat['name'] ?></a></li>
	    <? } ?>
	    </ul>
	    <div style="clear:both;border:none;margin:0;padding:0;"></div>
	  </div>
	<?php 
	}
	echo $this->Form->input('category_id',array('type'=>'hidden'));
	?>
	</div>

	<?php 
	echo $this->Form->input('url',array('type'=>'hidden'));
	echo $this->Form->input('blog_id',array('type'=>'hidden'));
	
	echo $this->Form->end(__('Submit', true)); ?>
	</fieldset>
<?php 
}else{
	echo $this->Form->create('Link');?>
	<fieldset>
 		<legend><?php __('Add Link'); ?></legend>
		<strong><?php __('Before add, please remember:') ?></strong>
    	<ul>
    		<li><? __('link to the original source: no link to sites who reblog a news') ?></li>
    		<li><? __('interesting content: will be interesting to a reasonable number of readers?') ?></li>
    		<li><? __('search before: to avoid duplicates') ?></li>
    		<li><? __('extern content: this is no a microblogging site or a a news generating site') ?></li>
    		<li><? __('be descriptived: write a news description faithfully, with no distortion') ?></li>
    		<li><? __('respects the vote of others: if the votes affects you personally, it is better not to send the news') ?></li>
		</ul><br />
	<?php 
		echo $this->Form->input('url',array('error' => array('escape' => false),'div'=>array('class'=>'input text required')));
	
		$options=array('text'=>$html->image('icon/news.png').'<br />Noticia','image'=>$html->image('icon/image.png').'<br />Imagen','video'=>$html->image('icon/video.png').'<br />Video');
		$attributes=array('div'=>true,'escape'=>false,'default'=>'text');
	?>
	
	<div id="radioType"><?=$form->radio('type',$options,$attributes) ?></div>
	<?=$this->Form->end(__('Submit', true)); ?>
	
	<script type="text/javascript">
	<?=$this->Js->get('#LinkAddForm')->event('submit','$.blockUI({ message: "<h1> Just a moment... we are fetching information from the url</h1>", css: { width:"1264px", height:"100%",top:"0px",left:"0px","background-color":"grayText",color:"#FFFFFF" } }); return true;'); ?> 
	</script>


	</fieldset>
	
<?php 
}
?>
</div>
